define(function () {
    var tdate = '';
    var self, vue,
        app = win.app;
    var events = [];
    //日历
    vue = new Vue({
        el: app.getByid(doc, 'calendar'),
        filters: {
            dayFilter: function (obj) {
                if (obj) {
                    if (obj instanceof String) {
                        return obj;
                    } else if (obj instanceof Object) {
                        if (obj.hasOwnProperty("singleDate")) {
                            return obj.singleDate + "";
                        } else {
                            return "";
                        }
                    } else {
                        return "";
                    }
                } else {
                    return "";
                }
            },
            dateFilter: function (date) {
                if (date) {
                    var str = date.toString()
                    return str.replace("T", " ")
                } else {
                    return "";

                }

            }
        },
        data: function () {
            return {
                monthsTitle: [
                    "一月",
                    "二月",
                    "三月",
                    "四月",
                    "五月",
                    "六月",
                    "七月",
                    "八月",
                    "九月",
                    "十月",
                    "十一月",
                    "十二月"
                ],
                weeksTitle: ["日", "一", "二", "三", "四", "五", "六"],
                selectedYear: "",
                selectedMonth: 0,
                daysOfYear: [],
                fullYear: true,
                dayHours: [
                    {text: "上午", hour: "12"},
                    {text: "上午", hour: "1"},
                    {text: "上午", hour: "2"},
                    {text: "上午", hour: "3"},
                    {text: "上午", hour: "4"},
                    {text: "上午", hour: "5"},
                    {text: "上午", hour: "6"},
                    {text: "上午", hour: "7"},
                    {text: "上午", hour: "8"},
                    {text: "上午", hour: "9"},
                    {text: "上午", hour: "10"},
                    {text: "上午", hour: "11"},
                    {text: "正午", hour: "12"},
                    {text: "下午", hour: "1"},
                    {text: "下午", hour: "2"},
                    {text: "下午", hour: "3"},
                    {text: "下午", hour: "4"},
                    {text: "下午", hour: "5"},
                    {text: "下午", hour: "6"},
                    {text: "下午", hour: "7"},
                    {text: "下午", hour: "8"},
                    {text: "下午", hour: "9"},
                    {text: "下午", hour: "10"},
                    {text: "下午", hour: "11"},
                    {text: "下午", hour: "12"}
                ],
                dayOfWeek: [],
                selectedWeek: 0,
                mode: 1,
                taskItem: [],
                //productSetItem: null,
                listItem: [],
                //taskSetItem: null,
                selectyear: '',
                tdate: tdate,
                active: 0,
                showLeft: false,
                showCenter: true,
                showRight: false
            };
        },

        created: function () {
            this.selectedYear = this.getCurrentYear();
            this.selectedMonth = this.getCurrentMonth();
        },
        activated: function () {
        },
        deactivated: function () {
        },
        destroyed: function () {
        },
        computed: {
            prevYear: function () {
                return this.selectedYear - 1;
            },
            nextYear: function () {
                return this.selectedYear + 1;
            },
            prevMonth: function () {
                if (this.selectedMonth === 0) {
                    return 11;
                }
                return this.selectedMonth - 1;
            },
            nextMonth: function () {
                if (this.selectedMonth === 11) {
                    return 0;
                }
                return this.selectedMonth + 1;
            },
            today: function () {
                return this.getToady()
            },
            currentHour: function () {
                return this.getCurrentHour();
            }
        },
        watch: {
            selectedYear: function (newVal, oldVal) {
                this.daysOfYear = this.getAllDaysOfYear(newVal);
                this.selectyear = newVal;
                this.getAllTask(newVal);
            }
        },
        methods: {
            getCurrentMonth: function () {
                var now = new Date();
                return now.getMonth();
            },
            onMonthDayClick: function (obj) {
                vue.selectedWeek = obj.week;
                tdate = obj.day.date;
                vue.tdate = tdate;
                this.getinfo();
                this.setWeekMode(obj.weekdays);

                mui('.mui-scroll-wrapper').scroll({
                    deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
                });
            },
            onWeekDayClick: function (obj) {
                tdate = obj.day.date;
                vue.tdate = tdate;
                this.getinfo();

                for (var i = 0; i < this.dayOfWeek.length; i++) {
                    var item = this.dayOfWeek[i];
                    if (item.checked && i != obj.index) {
                        item.checked = false;
                        this.$set(this.dayOfWeek, i, item);
                        break;
                    }
                }

                if (!obj.day.checked) {
                    obj.day.checked = true;
                }
                this.$set(this.dayOfWeek, obj.index, obj.day);
            },
            getDayClass: function (day) {
                if (day.date === this.today) {
                    //今天
                    if (day.checked) {
                        return 'active';
                    }
                    if (day.hasEvents) {
                        //今天又日程
                        return "schedule-day";//是今天又有日程的样式
                    } else {
                        //今天没日程
                        return "today";// 是今天但没日程
                    }

                } else {
                    //不是今天，
                    if (day.checked) {
                        return 'active';
                    } else {
                        if (day.hasEvents) {
                            //有日程
                            return "schedule-day";
                        } else {
                            //没有日程
                            return "";
                        }
                    }
                }
            },
            getTodayClass: function (day) {
                if (day.date === this.today) {
                    //今天
                    if (day.hasEvents) {
                        //今天又日程
                        return "schedule-day selected-day";//是今天又有日程的样式
                    } else {
                        //今天没日程
                        return "selected-day";// 是今天但没日程
                    }

                } else {
                    //不是今天，
                    if (day.hasEvents) {
                        //有日程
                        return "schedule-day";
                    } else {
                        //没有日程
                        return "";
                    }
                }
            },
            yearBtn: function (type) {
                mui('.mui-scroll-wrapper').scroll({
                    deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
                });
                this.mode = 0;
                setTimeout(function () {
                    mui('.mui-scroll-wrapper').scroll({
                        deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
                    });
                }, 1000);
                if (type === 'left') {
                    this.showLeft = true;
                    this.showCenter = false;
                    this.showRight = false;
                } else {
                    this.showLeft = false;
                    this.showCenter = true;
                    this.showRight = false;
                }
            },
            getCurrentHour: function () {
                return (new Date()).getHours();
            },
            getToady: function () {
                return new Date().Format('yyyy-MM-dd');
            },
            getCurrentYear: function () {
                return new Date().getFullYear();
            },
            getAllDaysOfYear: function (year) {
                var daysOfYear = [];
                for (var month = 0; month < 12; month++) {
                    daysOfYear.push(this.getAllDaysOfMonth(year, month));
                }
                return daysOfYear;
            },
            getAllDaysOfMonth: function (year, month) {
                //构造一个月的所有天数，日，一，二，三，四，五，六(0,1,2,3,4,5,6)
                var daysOfMonth = [];
                //当月第一天
                var firstDate = this.getUTCDate(year, month, 1);

                //当月最后一天
                var lastDate = this.getUTCDate(year, month + 1, 0);

                //获取当月第一天是周几
                var dayOfFirstDate = firstDate.getUTCDay();

                var dayOfLastDate = lastDate.getUTCDay();

                var arr = [];
                //补全开头几天
                if (dayOfFirstDate > 0 && dayOfFirstDate <= 6) {
                    for (var i = 0; i < dayOfFirstDate; i++) {
                        arr.push("start");
                    }
                }

                //中间有效日期
                var days = lastDate.getUTCDate();
                for (var day = 1; day <= days; day++) {
                    // 判断当天是否有日程
                    var thatDayHasEvents = false;
                    var daystr = year + "-" + (month + 1) + "-" + day;
                    for (var i = 0; i < events.length; i++) {
                        if (events[i] == daystr) {
                            thatDayHasEvents = true;
                        }
                    }

                    arr.push({
                        date: year + "-" + (month + 1) + "-" + day,
                        singleDate: day,
                        hasEvents: thatDayHasEvents
                    });

                }

                //补全后面几天
                if (dayOfLastDate >= 0 && dayOfLastDate < 6) {
                    for (var j = dayOfLastDate + 1; j <= 6; j++) {
                        arr.push("end");
                    }
                }

                //0 1 2 3  4  5  6
                //7 8 9 10 11 12 13
                for (var i = 0; i < arr.length / 7; i++) {
                    //一周7天
                    var weekDays = [];
                    for (var j = i * 7; j < (i + 1) * 7; j++) {
                        weekDays.push(arr[j]);
                    }
                    daysOfMonth.push(weekDays);
                }

                return daysOfMonth;

            },
            getUTCDate: function (year, month, date) {
                var d = new Date();
                d.setUTCFullYear(year);
                d.setUTCMonth(month);
                d.setUTCDate(date);
                d.setUTCHours(0);
                d.setUTCMinutes(0);
                d.setUTCSeconds(0);
                d.setUTCMilliseconds(0);
                return d;
            },
            selectPrevYear: function () {
                this.selectedYear = this.prevYear;
            },
            selectNextYear: function () {
                this.selectedYear = this.nextYear;
            },
            selectPrevMonth: function () {
                this.selectedMonth = this.prevMonth;
                if (this.selectedMonth === 11) {
                    this.selectedYear = this.prevYear;
                }
            },
            selectNextMonth: function () {
                this.selectedMonth = this.nextMonth;
                if (this.selectedMonth === 0) {
                    this.selectedYear = this.nextYear;
                }
            },
            onDayClick: function (obj) {
                this.selectedMonth = obj.month;
                this.mode = 1;
            },
            selectToday: function (type) {
                var arr = this.today.split("-");
                this.selectedYear = Number(arr[0]);
                this.setMonthMode(Number(arr[1] - 1));
                mui('.mui-scroll-wrapper').scroll({
                    deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
                });
                if (type === 'center') {
                    this.showLeft = false;
                    this.showCenter = true;
                    this.showRight = false;
                } else {
                    this.showLeft = false;
                    this.showCenter = true;
                    this.showRight = false;
                }
            },
            setYearMode: function (year) {
                this.selectedYear = year;
                this.mode = 0;
            },
            setMonthMode: function (month) {
                this.selectedMonth = month;
                this.mode = 1;

            },
            setWeekMode: function (week) {
                this.dayOfWeek = week;
                this.mode = 2;
            },
            getAllTask: function (newVal) {
                var list = vue.listItem;
                app.ajax({
                    xurl: "api/wx/gwat151/getAllTask",
                    cb: function (result) {
                        if (result.ok) {

                            if (result.data.data.length == 0) {

                            } else {
                                events.length = 0;
                                list.pushArray(result.data.data, true);
                                for (var i = 0; i < list.length; i++) {
                                    if (list[i].GWBA003) {

                                        var str = list[i].GWBA003.substring(0, 10);
                                        arr = str.split("-");
                                        var y = arr[0];
                                        var m = arr[1];
                                        if (m.substring(0, 1) == "0") {
                                            m = m.substring(1, 2)
                                        }
                                        var d = arr[2];
                                        if (d.substring(0, 1) == "0") {
                                            d = d.substring(1, 2)
                                        }
                                        events.push(y + "-" + m + "-" + d)
                                    }
                                }
                                vue.daysOfYear = vue.getAllDaysOfYear(newVal);
                                vue.dayOfWeek = vue.daysOfYear[vue.selectedMonth][vue.selectedWeek];
                            }
                        }
                    }
                });
            },
            getinfo: function () {
                if (tdate.length != "") {
                    //拼接日期 2018-1-1 → 2018-01-01
                    arr = tdate.split("-");
                    if (arr[1].length == "1") {
                        arr[1] = "0" + arr[1]
                    }
                    if (arr[2].length == "1") {
                        arr[2] = "0" + arr[2]
                    }
                    tdate = arr[0] + "-" + arr[1] + "-" + arr[2]
                }
                var task = vue.taskItem;
                app.ajax({
                    xurl: "api/wx/gwat151/getGwba",
                    xdata: {
                        DATE: tdate
                    },
                    cb: function (result) {
                        if (result.ok) {
                            if (result.data.data.length == 0) {
                                vue.taskItem = [{GWBA001: tdate, GWBA003: "-", GWBA004: "-", GWBA005: "创建日程"}]

                            } else {
                                task.length = 0;
                                task.pushArray(result.data.data, false);
                            }
                        }
                    }
                });
            },
            showTaskInfo: function (item, day) {
                app.openPage({
                    src: "gwat151/gwat151-info.html",
                    argsKey: 'gwat151-Taskinfo',
                    args: {
                        item: item,
                        cb: function () {
                            events.length = 0;
                            vue.getinfo();
                            vue.getAllTask(vue.selectyear);
                        }
                    }
                });
            },
            addDetails: function (item, type) {
                if (tdate.length != "") {
                    //拼接日期 2018-1-1 → 2018-01-01
                    arr = tdate.split("-");
                    if (arr[1].length == "1") {
                        arr[1] = "0" + arr[1]
                    }
                    if (arr[2].length == "1") {
                        arr[2] = "0" + arr[2]
                    }
                    tdate = arr[0] + "-" + arr[1] + "-" + arr[2]
                } else {
                    tdate = new Date().Format("yyyy-MM-dd")
                }
                app.openPage({
                    src: "gwat151/gwat151-info-add.html",
                    argsKey: 'gwat151-info-add',
                    args: {
                        date: tdate,
                        cb: function () {
                            events.length = 0;
                            vue.getinfo();
                            vue.getAllTask(vue.selectyear);
                        }
                    }
                });
                if (type === 'right') {
                    this.showLeft = false;
                    this.showCenter = false;
                    this.showRight = true;
                } else {
                    this.showLeft = false;
                    this.showCenter = true;
                    this.showRight = false;
                }
            }
        }
    });

    mui('.mui-scroll-wrapper').scroll({
        deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
    });

});